/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.hutech09.jsale.web.manager.category;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import org.hutech09.jsale.bsn.ICategoryManager;
import org.hutech09.jsale.bsn.IProductManager;
import org.hutech09.jsale.bsn.exception.ItemNotFoundException;
import org.hutech09.jsale.entity.Category;
import org.primefaces.event.SelectEvent;

/**
 *
 * @author Nam
 */
@ManagedBean
@SessionScoped
public class CategoryBrowseCtrl {

    @EJB
    private IProductManager pm;

    @EJB
    private ICategoryManager cm;

    @ManagedProperty("#{productTypeBrowseCtrl}")
    private ProductTypeBrowseCtrl typeCtrl;
    
    private List<Category> categories;

    private Category selectedCategory;

    @PostConstruct
    public void init() {
        categories = cm.getAllCategory();
    }

    public List<Category> getCategories() {
        return categories;
    }
        
    public void onRowSelect(SelectEvent event) {
    }

    public void setCategories(List<Category> categories) {
        this.categories = categories;
    }

    public Category getSelectedCategory() {
        return selectedCategory;
    }
    
    public void delete(){
        if(selectedCategory!=null){
            try {
                if(cm.isCategoryEmpty(selectedCategory.getId()))
                    cm.deleteCategory(selectedCategory.getId());
                init();
            } catch (ItemNotFoundException ex) {
                Logger.getLogger(CategoryBrowseCtrl.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void setSelectedCategory(Category selectedCategory) {
        this.selectedCategory = selectedCategory;
        typeCtrl.setCategory(selectedCategory);
    }

    public ProductTypeBrowseCtrl getTypeCtrl() {
        return typeCtrl;
    }

    public void setTypeCtrl(ProductTypeBrowseCtrl typeCtrl) {
        this.typeCtrl = typeCtrl;
    }
    
    
}
